package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.AlbumObject;
import saucaca.Object.HomeData;
import saucaca.Object.NewsObject;
import saucaca.Object.SlideObject;
import saucaca.Object.SongObject;
import saucaca.Util.CMD;
import saucaca.Util.TagName;
import saucaca.fragment.AlbumDetailFargment;
import saucaca.fragment.NewDetailFragment;
import saucaca.fragment.home;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ImageSlideAdapter extends PagerAdapter {
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	// private ImageLoadingListener imageListener;
	Context context;
	List<SlideObject> videos;
	home newhomeFragment;
	String Slide[]=new String[15];
	String URL="0";
	String SONG="1";
	String VIDEO="2";
	String ALBUM="3";
	String NEWS="4";
	
	public ImageSlideAdapter(Context context, List<SlideObject> videos) {
		this.context = context;
		this.videos = videos;
		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default).cacheInMemory().cacheOnDisc().build();
		// imageListener = new ImageDisplayListener();
		for (int i = 0; i < Slide.length; i++) {
			Slide[i]="http://api.androidhive.info/json/movies/"+(i+1)+".jpg";
		}
	}

	@Override
	public int getCount() {
		return videos.size();
	}

	@Override
	public View instantiateItem(ViewGroup container, final int position) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.item_home_viewpager_image, container, false);

		ImageView mImageView = (ImageView) view.findViewById(R.id.image_display);
		mImageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				if(BaseEngine.convertStringLongtoInteger(videos.get(position).getOTYPE()).equals(URL)){
					Intent i = new Intent(Intent.ACTION_VIEW);
					i.setData(Uri.parse(videos.get(position).getOBJECT_ID()));
					context.startActivity(i);
				}else if(BaseEngine.convertStringLongtoInteger(videos.get(position).getOTYPE()).equals(ALBUM)){
					AlbumObject albumitem=new AlbumObject();
					albumitem.setALBUM_ID(videos.get(position).getOBJECT_ID());
					((MainActivity) context).switchContent(AlbumDetailFargment.class.getName(), albumitem);
				}else if(BaseEngine.convertStringLongtoInteger(videos.get(position).getOTYPE()).equals(VIDEO)){
					SongObject video=new SongObject();
					video.setSONG_ID(videos.get(position).getOBJECT_ID());
					BaseEngine.setVideoclick(context, video);
				}else if(BaseEngine.convertStringLongtoInteger(videos.get(position).getOTYPE()).equals(NEWS)){
					NewsObject newsObject=new NewsObject();
					newsObject.setAUTO_ID(videos.get(position).getOBJECT_ID());
					((MainActivity) context).switchContent(NewDetailFragment.class.getName(), newsObject);
				}else{
					new songDetailsServices().execute(videos.get(position).getOBJECT_ID());
				}
			}
		});
		imageLoader.displayImage(((SlideObject) videos.get(position)).getAVATAR(), mImageView, options);
//		imageLoader.displayImage(Slide[position], mImageView, options);
		container.addView(view);
		return view;
	}
	
	class songDetailsServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.idDetais(CMD.CMD_SONG_DETAIL, params[0]);
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					JsonReader.readHeader(result);
					if(HomeData.headerObject.getCODE().equals("0")){
						ArrayList<SongObject> listsongs = JsonReader.readSongdetail(result);
						if (listsongs != null	&& listsongs.size()>0){
							SongObject songObject=listsongs.get(0);
							try {
								((MainActivity) context).mPlaylistMgr.add(songObject);
								((MainActivity) context).play(((MainActivity) context).mPlaylistMgr.getArrayList().size()-1);
							} catch (Exception e) {
								ArrayList<SongObject> lists = new ArrayList<SongObject>();
								lists.add(songObject);
								((MainActivity) context).SendPlaylistAndplay(lists, 0);
							}
						}
					}else{
						TagName.showToastShort(context, "Lỗi id silde: "+ HomeData.headerObject.getMESSAGE());
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}
}